/*
 * Copyright 2009-2010 Belmont Software Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.belmont.backup.server;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.mortbay.jetty.security.*;
import org.mortbay.jetty.servlet.*;
import com.belmont.backup.*;

public class BackupAdminServlet extends DefaultServlet implements IBackupConstants {
    DigestAuthenticator authenticator;

    public void init(ServletConfig sconfig) throws ServletException {
        super.init(sconfig);
        authenticator = new DigestAuthenticator();
        Utils.log(LOG_INFO, "BackupAdminServlet initialized");
    }

    public void destroy() {
        Utils.log(LOG_INFO, "BackupAdminServlet destroy");
    }
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        Utils.log(LOG_INFO, "admin doGet "+req.getRequestURL());
        try {
            String pathInfo = req.getServletPath();

            //Utils.log(LOG_INFO, "BackupAdminServlet doGet "+pathInfo);

            if (pathInfo.equals("/")) {
                resp.sendRedirect("/ui/BackupUI.html");
            } else if (pathInfo.startsWith("/ui")) {
                super.doGet(req, resp);
            } else {
                resp.sendError(401,"Not Authorized");
            }
        } catch (ServletException ex) {
            Utils.log(LOG_ERROR, "Servlet exception", ex);
        } catch (IOException ex) {
            Utils.log(LOG_ERROR, "Servlet exception", ex);
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String pathInfo = req.getPathInfo().toLowerCase();

            Utils.log(LOG_INFO, "BackupAdminServlet doPost "+pathInfo);
            resp.sendError(401,"Not Authorized");
        } catch (IOException ex) {
            Utils.log(LOG_ERROR, "Servlet exception", ex);
        }
    }

    protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req, resp);
    }
}
